{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 1. Visualizations, Part 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this notebook, we continue to look at methods of displaying information visually. See [Part 1](Visualization.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#Table of Contents\n", "* [1. Visualizations, Part 2](#1.-Visualizations,-Part-2)\n", "\t* [1.1 Preliminaries](#1.1-Preliminaries)\n", "\t\t* [1.1.1 Arrays](#1.1.1-Arrays)\n", "\t* [1.2 Albers Conic Map Projection](#1.2-Albers-Conic-Map-Projection)\n", "\t* [1.3 Genomics](#1.3-Genomics)\n", "\t* [1.4 Relationships](#1.4-Relationships)\n", "\t* [1.5 Heatmaps](#1.5-Heatmaps)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.1 Preliminaries" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To explore these ideas, we need to introduce a new construct in Processing: the array." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.1.1 Arrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Arrays are contiguous blocks of memory used to store similar things. Effectively this allows you to have variables that you can reference by a number.\n", "\n", "You can define a variable to hold an unspecified number of integers with:\n", "\n", "```java\n", "int [] values = new int[] { 1, 2, 3 };\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also assign the variable with an array of values at the same time. We use the `new` keyword to create the array that holds the elements 1, 2, and 3:\n", "\n", "\n", "```java\n", "int [] values = new int[] { 1, 2, 3 };\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To refer to the first and second items, we use:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```java\n", "values[0]\n", "values[i]\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's see the array in use." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.2 Albers Conic Map Projection" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the last lecture, we used a SVG image of the united states.\n", "\n", "Here, we use a meta-command to download the file again:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%download http://upload.wikimedia.org/wikipedia/commons/archive/3/32/20091105194402%21Blank_US_Map.svg" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And a meta-command to rename the file:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "! mv 20091105194402%21Blank_US_Map.svg usa-wikipedia.svg" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this example, we want to use the states as before, but also be able to plot longitude and latitude on the map.\n", "\n", "Since the map was just a picture (and not drawn) I wasn't sure what coordinate system it was in.\n", "\n", "After a bit of trial and error, I found that the Albers conic projection was a good fit.\n", "\n", "In this example, I plot some longitude and latitude points, and kept adjusting the xoffset, xscale, yoffset, and yscale until the points lined up pretty well. Normally, we would know these values, but the wikipedia map did not provide those.\n", "\n", "If you click, it will plot those points, and some cites.\n", "\n", "Notice that the albers function returns an array containing the x and the y values:" ] }, { "cell_type": "code", "execution_count": 88, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " Sketch #82:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #82 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "PShape usa;\n", "PShape michigan;\n", "PShape ohio;\n", "\n", "void setup() {\n", " size(959, 593); \n", " background(255);\n", " usa = loadShape(\"usa-wikipedia.svg\");\n", " michigan = usa.getChild(\"MI\");\n", " ohio = usa.getChild(\"OH\");\n", "}\n", "\n", "void draw() { \n", " // Draw the full map\n", " shape(usa, 0, 0); \n", " noLoop();\n", "}\n", "\n", "float[] albers(lat, lng) {\n", " lat0 = 23.0 * (PI/180); // Latitude_Of_Origin\n", " lng0 = -96.0 * (PI/180); // Central_Meridian\n", " phi1 = 30.0 * (PI/180); // Standard_Parallel_1\n", " phi2 = 50.0 * (PI/180); // Standard_Parallel_2\n", "\n", " n = 0.5 * (sin(phi1) + sin(phi2));\n", " c = cos(phi1);\n", " C = c * c + 2 * n * sin(phi1);\n", " p0 = sqrt(C - 2 * n * sin(lat0)) / n;\n", " theta = n * (lng * PI/180 - lng0);\n", " p = sqrt(C - 2 * n * sin(lat * PI/180)) / n;\n", " x = p * sin(theta);\n", " y = p0 - p * cos(theta);\n", " return new float[2] { x, y };\n", "}\n", "\n", "void plot(lat, lon, c) {\n", " // Values to scale the lat, lon to fit on the USA map:\n", " xoffset = 485;\n", " xscale = 1245;\n", " yoffset = 630; \n", " yscale = 1250;\n", " \n", " float[2] xy = albers(lat, lon);\n", " fill(c);\n", " ellipse(xoffset + xy[0] * xscale, yoffset - xy[1] * yscale, 10, 10);\n", "}\n", "\n", "void mousePressed() {\n", " for (lat = 30; lat <= 50; lat += 5) {\n", " for (lon = 70; lon <= 130; lon += 5) {\n", " plot(lat, -lon, color(255));\n", " }\n", " }\n", " // Some Cites:\n", " plot(39.790942, -86.147685, color(255, 0, 128));\n", " plot(47.042418, -122.893077, color(255, 128, 0));\n", " plot(30.4518, -84.27277, color(255, 0, 0));\n", " plot(44.323535, -69.765261, color(255, 0, 255));\n", " plot(33.448457, -112.073844, color(255, 255, 0));\n", "}\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can use this to overlay data, such as population density, locations of events, etc. You might want to use the 4th element of color(), the alpha value, to make the colors transparent." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.3 Genomics" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is a diagram style that is used a lot in genomic and biological data: \n", "\n", "http://circos.ca/intro/genomic_data/\n", "\n", "\n", "\n", "\n", "How could we make such a chart?\n", "\n", "First, let's make some tests using different curves.\n", "\n", "I found that the bezier, with control points at the center looks pretty good:" ] }, { "cell_type": "code", "execution_count": 98, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " Sketch #92:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #92 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "noFill();\n", "stroke(255, 0, 0);\n", "bezier(10, 10, 50, 50, 50, 50, 90, 10);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For this example, we'll reuse the rotateAround functions from the last assignment. This is used to find the starting and stopping points around a center.\n", "\n", "Then, we draw a bezier curve between them:" ] }, { "cell_type": "code", "execution_count": 109, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " Sketch #103:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #103 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "int rotateAroundX(int x1, int y1, int length, int angle) {\n", " return x1 + length * cos(angle);\n", "}\n", "\n", "int rotateAroundY(int x1, int y1, int length, int angle) {\n", " return y1 - length * sin(angle);\n", "}\n", "\n", "void setup() {\n", " size(300, 300);\n", " background();\n", "}\n", "\n", "void draw() {\n", " // Center:\n", " cx = width/2;\n", " cy = height/2;\n", " \n", " // Random degree, converted to radians:\n", " p1 = random(360) * PI/180;\n", " // Random degree, converted to radians:\n", " p2 = random(360) * PI/180;\n", "\n", " // Find where p1 would be:\n", " x1 = rotateAroundX(cx, cy, width/2, p1);\n", " y1 = rotateAroundY(cx, cy, width/2, p1);\n", " \n", " // Find where p2 would be:\n", " x2 = rotateAroundX(cx, cy, width/2, p2);\n", " y2 = rotateAroundY(cx, cy, width/2, p2);\n", " \n", " // Connect p1 to p2:\n", " noFill();\n", " stroke(random(255), random(255), random(255));\n", " bezier(x1, y1, cx, cy, cx, cy, x2, y2);\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What could you use this to represent?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.4 Relationships" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this example, we construct three arrays:\n", "\n", "* name - to hold the name of a person\n", "* from - to hold the index of the to-person\n", "* to - to hold the index of the from-person\n", "\n", "The idea is we are representing information about a relationship. For example, it might represent a tweet (from one person, sent to (or mentioning) another)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " Sketch #1:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #1 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "int rotateAroundX(int x1, int y1, int length, int angle) {\n", " return x1 + length * cos(angle);\n", "}\n", "\n", "int rotateAroundY(int x1, int y1, int length, int angle) {\n", " return y1 - length * sin(angle);\n", "}\n", "\n", "void setup() {\n", " size(300, 300);\n", " background();\n", "}\n", "\n", "void draw() {\n", " // Center:\n", " cx = width/2;\n", " cy = height/2;\n", " \n", " int[] from = new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };\n", " int[] to = new int[] {1, 3, 3, 6, 3, 1, 2, 1, 2, 3 };\n", " string[] name = new string[] {\"Helen\", \n", " \"Mary\",\n", " \"Teyvonia\",\n", " \"Glenda\",\n", " \"Bertrude\",\n", " \"Elvie\",\n", " \"Sarah\",\n", " \"Mary\",\n", " \"Trisha\",\n", " \"Karen\" };\n", "\n", " // First, let's draw the names around the circle:\n", " for (int i = 0; i < 10; i++) {\n", " p1 = from[i] * 36 * PI/180;\n", " p2 = to[i] * 36 * PI/180;\n", " \n", " x1 = rotateAroundX(cx, cy, width/2, p1);\n", " y1 = rotateAroundY(cx, cy, width/2, p1);\n", " fill(0);\n", " text(name[i], x1, y1);\n", " }\n", " // Next, we connect up the people:\n", " for (int i = 0; i < 10; i++) {\n", " p1 = from[i] * 36 * PI/180;\n", " p2 = to[i] * 36 * PI/180;\n", "\n", " // Find where p1 would be:\n", " x1 = rotateAroundX(cx, cy, width/2, p1);\n", " y1 = rotateAroundY(cx, cy, width/2, p1);\n", "\n", " // Find where p2 would be:\n", " x2 = rotateAroundX(cx, cy, width/2, p2);\n", " y2 = rotateAroundY(cx, cy, width/2, p2);\n", "\n", " \n", " // Connect p1 to p2:\n", " noFill();\n", " strokeWeight(random(5));\n", " stroke(random(255), random(255), random(255));\n", " bezier(x1, y1, cx, cy, cx, cy, x2, y2);\n", " }\n", " noLoop();\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this example, I randomly drew the connecting line in a random color and weight. That information could also be stored in an array." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.5 Heatmaps\n", "\n", "The idea of a heatmap is to show intensity as a visual representation, often color. Here we see the number of discussions between two people represented as a heatmap. Why is the diagonal white?\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "http://www.sharkalytics.com/heatmap" ] } ], "metadata": { "kernelspec": { "display_name": "Calysto Processing", "language": "processing", "name": "calysto_processing" }, "language_info": { "codemirror_mode": { "name": "text/x-java", "version": 2 }, "file_extension": ".java", "mimetype": "text/x-java", "name": "java" } }, "nbformat": 4, "nbformat_minor": 0 }